﻿package common.dao.connection;

import java.io.IOException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import util.StringUtil;

/**
 * 数据库连接提供器的实现类
 */
public class ConnectionProvider implements IConnectionProvider {

	public static String CONNECTION_CONFIG_FILE = "/connection.properties";//连接配置文件

	private static Properties result;//连接内容

	private Connection connection;

	public Connection getConnection() throws Exception {
		Properties result = getProperties();
		String dsName = result.getProperty("connection.dsname");//数据源的名称
		if(!StringUtil.isNull(dsName)) {
			this.connection = this.getConnectionByDs("java:comp/env/" + dsName);
		} else {
			this.connection = getConnectionByConfig();
		}
		return this.connection;
	}

	public Object close()throws Exception {
		try {
			if (this.connection != null) {
				this.connection.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}


	private Connection getConnectionByConfig() throws Exception {
		String className = result.getProperty("connection.driver_class");
		String url = result.getProperty("connection.url");
		String userName = result.getProperty("connection.username");
		String password = result.getProperty("connection.password");
		
		Properties dbProps = new Properties();
		dbProps.put("user", userName);
		dbProps.put("password", password);

		Driver driver = null;
		try {
			driver = (Driver) Class.forName(className).newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		

		Connection connection = null;
		try {
			DriverManager.registerDriver(driver);
			connection = DriverManager.getConnection(url, userName, password);
			//connection = driver.connect(url, dbProps);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return connection;
	}

	/**
	 * 
	 * 根据数据源获取连接
	 * 
	 * @param name
	 *            数据源的路径，如:java:comp/env/jdbc/mydb
	 * @return
	 * 
	 */
	private Connection getConnectionByDs(String name) {
		Context context = null;
		DataSource ds = null;
		try {
			context = new InitialContext();
			ds = (DataSource) context.lookup(name);
		} catch (NamingException e) {
			e.printStackTrace();
		}

		Connection con = null;
		try {
			con = ds.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return con;
	}

	//读取连接配置
	private Properties getProperties() throws Exception {
		if (result != null) {
			return result;
		}

		result = new Properties();
		try {
			result.load(this.getClass()
					.getResourceAsStream(CONNECTION_CONFIG_FILE));
		} catch (IOException e) {
			e.printStackTrace();
		}

		return result;
	}
}
